<template>
  <label
    for="name"
    class="lm-switch"
    @click="handleClick"
    :class="{ 'is-checked': value }"
  >
    <!-- 切换的小球 -->
    <span class="lm-switch_core" ref="core">
      <span class="lm-switch_button"> </span>
      <div v-if="value" class="lm-switch_button_open">
        {{ openText }}
      </div>
      <div v-if="!value" class="lm-switch_button_close">
        {{ closeText }}
      </div>
    </span>

    <input type="checkbox" class="lm-switch_input" :name="name" ref="input" />
  </label>
</template>

<script>
export default {
  name: "LmSwitch",
  props: {
    value: {
      type: Boolean,
      default: true,
    },
    activeColor: {
      type: String,
      default: "#3872F0",
    },
    unActiveColor: {
      type: String,
      default: "#5582C2",
    },
    name: {
      type: String,
      default: "username",
    },
    color: {
      type: String,
      default: "#ffffff",
    },
    openText: {
      type: String,
      default: "",
    },
    closeText: {
      type: String,
      default: "",
    }
  },
  watch: {
    value(newVal) {
      this.setColor();
    },
  },
  mounted() {
    //   初始化开关的颜色
    if (this.activeColor || this.unActiveColor) {
      this.setColor();
    }
    // 控制checkbox的值，input值同步到value值
    this.$refs.input.checked = this.value;
  },
  methods: {
    setColor() {
      let color = this.value ? this.activeColor : this.unActiveColor;
      let textColor = this.color;
      this.$refs.core.style.borderColor = color;
      this.$refs.core.style.backgroundColor = color;
      this.$refs.core.style.color = textColor;
    },
    handleClick(e) {
      this.$emit("input", !this.value);
      //   控制checkbox的值，input值同步到value值
      this.$refs.input.checked = this.value;
    },
  },
};
</script>

<style lang="scss" scoped>
.lm-switch {
  display: inline-block;
  align-items: center;
  position: relative;
  font-size: 14px;
  line-height: 20px;
  vertical-align: middle;
  .lm-switch_core {
    margin: 0;
    display: inline-block;
    position: relative;
    width: 50px;
    height: 20px;
    border: 1px solid #dcdfe6;
    outline: none;
    border-radius: 10px;
    box-sizing: border-box;
    background: #dcdfe6;
    cursor: pointer;
    transition: border-color 0.3s, background-color 0.3s;
    vertical-align: middle;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    .lm-switch_button {
      position: absolute;
      top: 1px;
      left: 1px;
      border-radius: 100%;
      transition: all 0.3s;
      width: 16px;
      height: 16px;
      background-color: #fff;
    }
    .lm-switch_button_close {
      position: absolute;
      left: 18px;
    }
  }
}
// 选中样式
.is-checked {
  .lm-switch_core {
    border-color: #409eff;
    background-color: #409eff;
    .lm-switch_button {
      transform: translateX(30px);
    }
    .lm-switch_button_open {
      position: absolute;
      transform: translateX(5px);
    }
  }
}
// 隐藏input标签
.lm-switch_input {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  margin: 0;
}
</style>